﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Core;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using System.Data.SqlClient;

using Jungle.Prodamo.Core;
using Jungle.Prodamo.MostNeedFunctions;
using Jungle.Prodamo.PaySystem;

public partial class user : BaseUserModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 1960; i < 2008; i++)
        {
            ddl_Year.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }

    protected void SendRegisterRequest(object sender, EventArgs e)
    {
        UserManager um = new UserManager();
        um.NickName = tb_NickName.Text.Trim();

        bool valid = true;
        ValidBlock.InnerHtml = "";
        if (!um.CheckNickName()) { valid = false; ValidBlock.InnerHtml += "Пользователь с таким логином уже зарегистрирован<br/>"; }
        if (tb_NickName.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Логин<br/>"; }
        if (tb_Fname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Фамилию<br/>"; }
        if (tb_Iname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Имя<br/>"; }
        if (tb_Oname.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Отчество<br/>"; }
        if (tb_Email.Text.Trim().Length < 1 || tb_Email.Text.IndexOf("@") == -1) { valid = false; ValidBlock.InnerHtml += "Укажите Email<br/>"; }
        if (tb_Zip.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Zip(почтовый код)<br/>"; }
        if (tb_Address.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Адрес<br/>"; }
        if (rb_ur.Checked)
        {
            if (tb_Organization.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите Название Организации<br/>"; }
            if (tb_Erdpou.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите код ЕДРПОУ<br/>"; }
        }
        if (tb_Password.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Укажите пароль<br/>"; }
        if (tb_Repassword.Text.Trim().Length < 1) { valid = false; ValidBlock.InnerHtml += "Повторите в пароль<br/>"; }
        if (tb_Password.Text.Trim() != tb_Repassword.Text.Trim()) { valid = false; ValidBlock.InnerHtml += "Пароли не совпадают<br/>"; }


        um.GetAll();
        um.Email = tb_Email.Text.Trim();
        bool valid2 = true;
        foreach (UserManager u in um.UserList)
        {
            if (u.Email == um.Email)
            {
                valid2 = false;
                break;
            }
        }
        if (valid2)
        {
            if (valid)
            {
                um.Fname = tb_Fname.Text.Trim();
                um.Iname = tb_Iname.Text.Trim();
                um.Oname = tb_Oname.Text.Trim();
                um.Email = tb_Email.Text.Trim();
                switch (ddl_Sex.SelectedValue)
                {
                    case "1": um.Sex = true; break;
                    case "0": um.Sex = false; break;
                    default: um.Sex = true; break;
                }

                if (rb_ur.Checked)
                {
                    um.Organization = tb_Organization.Text.Trim();
                    um.Edrpou = tb_Erdpou.Text.Trim();
                    um.WorkTime = tb_WorkTime.Text.Trim();
                    um.Web = tb_Web.Text.Trim();
                    if (fu_Logo.HasFile)
                    {
                        fu_Logo.PostedFile.SaveAs(Server.MapPath("Images/Firms/" + Session.SessionID + "_" + fu_Logo.FileName));
                        um.Logo = "~/Images/Firms/" + Session.SessionID + "_" + fu_Logo.FileName;
                    }
                    else
                        um.Logo = "~/Images/1px.gif";
                    

                }
                else
                {
                    um.Organization = "NONE";
                    um.Edrpou = "NONE";
                    um.WorkTime = "NONE";
                    um.Web = "NONE";
                    um.Logo = "NONE";
                }
                um.Country = ddl_Country.SelectedValue;
                um.Zip = tb_Zip.Text.Trim();
                um.City = ddl_City.SelectedValue;
                um.Address = tb_Address.Text.Trim();
                um.Phone = tb_Phone.Text.Trim();
                um.Faks = tb_Faks.Text.Trim();
                um.BirthDay = Convert.ToDateTime(ddl_Day.Value + "." + ddl_Month.Value + "." + ddl_Year.SelectedValue);
                um.Password = Functions.getMd5Hash(tb_Repassword.Text.Trim());
                um.FastKey = GetRandom("");
                if (rb_fiz.Checked) um.UsertType = true;
                else um.UsertType = false;

                um.Map = "~/Images/1px.gif";
                um.Deliver = "";
                um.Credit = "";

                um.Add();
                string password = tb_Repassword.Text.Trim();

                string body = "Вы были зарегистрированы на prodamo.com. Данные для доступа: Login:" + um.NickName + ". Password: " + password + ". Ключ быстрого доступа: " + um.FastKey;
                um.SendEmail(um.Email, "prodamo.com: подтверждение регистрации", body);

                Account acc = new Account();
                acc.CreateAccount(um.UserId);

                if (um.Login())
                {
                    SessionKeeper newSession = new SessionKeeper();
                    newSession.SessionId = Session.SessionID.ToString();
                    newSession.UserId = um.UserId;
                    newSession.UserLogin = um.NickName;
                    newSession.AdminLogin = "none";
                    newSession.Create();
                    Response.Redirect(UrlHelper.GetSiteUrl());
                }


            }
        }
        else
            ValidBlock.InnerHtml += "Пользователь с таким email уже зарегистрирован<br/>";
    }

    protected string GetRandom(string str)
    {
        string mask = "1234567890qwertyuiopasdfghjklzxcvbnm";
        System.Random rnd = new Random();
        for (int i = 0; i < 6; i++)
        {
            str += mask[rnd.Next(0, 35)];
        }
        return str;
    }
}
